package com.tvserials.notification;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.view.MenuItem;
import com.tvserials.notification.preferences.SharedPreferencesUtils;

public class UserSettingActivity extends PreferenceActivity {

    private static int prefs = R.xml.settings;

    public UserSettingActivity() {
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            getClass().getMethod("getFragmentManager");
            AddResourceApi11AndGreater();
        } catch (NoSuchMethodException e) {
            AddResourceApiLessThan11();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                int SUCCESS_RESULT = 1;
                setResult(SUCCESS_RESULT, new Intent());
                finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @SuppressWarnings("deprecation")
    protected void AddResourceApiLessThan11() {
        addPreferencesFromResource(prefs);
    }


    @TargetApi(11)
    protected void AddResourceApi11AndGreater() {
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PF()).commit();

    }

    @TargetApi(11)
    public class PF extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(UserSettingActivity.prefs);

            updateStyle();
            updateVersion();
            updateName();
        }

        private void updateName() {
            Preference prefName = findPreference("prefName");
            if (prefName != null) {
                String user = SharedPreferencesUtils.getUser(getApplicationContext());
                prefName.setSummary(user);
            }
        }

        private void updateVersion() {
            try {
                String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
                Preference prefVersion = findPreference("prefVersion");
                if (prefVersion != null) {
                    prefVersion.setSummary(versionName);
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        /**
         * Workaround to apply default styles for custom preferences
         */
        private void updateStyle() {
            Preference prefIndicator = findPreference("prefIndicator");
            if (prefIndicator != null) {
                Preference prefStart = findPreference("prefStart");
                if (prefStart != null) {
                    prefStart.setLayoutResource(prefIndicator.getLayoutResource());
                }

                Preference prefEnd = findPreference("prefEnd");
                if (prefEnd != null) {
                    prefEnd.setLayoutResource(prefIndicator.getLayoutResource());
                }
            }
        }
    }

}